package com.bjsxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.Config;
import com.bjsxt.model.Result;
import com.bjsxt.service.ConfigService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * description:
 * 参数管理控制层
 * 参数管理的权限:
 * 查询： config_query
 * 修改： config_update
 * 新增： config_create
 * 删除： config_delete
 *
 * @author ALIENWARE <br />
 * @date 2021/7/26
 */
@RestController
@RequestMapping("/configs")
@Api(tags = "参数管理控制器")
public class ConfigController {
    /**
     * 参数管理的业务层接口
     */
    @Autowired
    private ConfigService configService;

    /**
     * 分页查询平台配置数据
     *
     * @param page 分页对象<Config>
     * @param type 配置规则类型
     * @param code 配置规则代码
     * @param name 配置规则名称
     * @return 统一响应对象
     */
    @GetMapping
    @PreAuthorize("hasAuthority('config_query')")
    @ApiOperation(value = "分页查询平台配置数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Long", paramType = "query", example = "20"),
            @ApiImplicitParam(name = "type", value = "配置规则类型", dataType = "String", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "code", value = "配置规则代码", dataType = "String", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "name", value = "配置规则名称", dataType = "String", paramType = "query", example = "配置规则")
    })
    public Result<Page<Config>> findByPage(@ApiIgnore Page<Config> page, String type, String code, String name) {
        // 调用业务层进行分页数据的查询
        Page<Config> configPage = configService.pageByTypeAndCodeAndName(page, type, code, name);
        // 返回响应数据
        return Result.success(configPage);
    }

    /**
     * 根据配置 ID 获取平台配置详情数据
     *
     * @param id 配置 ID
     * @return 统一响应对象
     */
    @GetMapping("/info")
    @PreAuthorize("hasAuthority('config_query')")
    @ApiOperation(value = "获取平台配置详情", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "Config 配置 id", dataType = "Integer", required = true, paramType = "query", example = "1")
    )
    public Result<Config> getById(@NotNull Long id) {
        // 根据 ID 查询 Config 对象
        Config config = configService.getById(id);
        // 返回响应数据
        return Result.success(config);
    }

    /**
     * 新增一个平台参数配置
     *
     * @param config 平台配置信息对象
     * @return 统一响应对象
     */
    @PostMapping
    @PreAuthorize("hasAuthority('config_create')")
    @ApiOperation(value = "新增一个平台参数配置", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "config", value = "Config 的 json 数据", dataType = "String", paramType = "body", example = "{}"),
    })
    public Result<Void> add(@RequestBody @Validated Config config) {
        // 调用业务层方法进行数据的保存
        boolean flag = configService.save(config);
        // 如果为 true 新增成功
        if (flag) return Result.success("新增成功");
        // 如果为 false 新增失败
        return Result.fail("新增失败");
    }

    /**
     * 更新一个平台参数配置
     *
     * @param config 平台配置信息对象
     * @return 统一响应对象
     */
    @PatchMapping
    @PreAuthorize("hasAuthority('config_update')")
    @ApiOperation(value = "更新一个平台参数配置", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "config", value = "Config 的 json 数据", dataType = "String", paramType = "body", example = "{}")
    })
    public Result<Void> update(@RequestBody @Validated Config config) {
        // 调用业务层方法进行数据的保存
        boolean flag = configService.updateById(config);
        // 如果为 true 更新成功
        if (flag) return Result.success("更新成功");
        // 如果为 false 更新失败
        return Result.fail("更新失败");
    }

    /**
     * 根据平台参数配置 ID 集合删除平台参数配置
     *
     * @param ids 配置 ID 集合
     * @return 统一响应对象
     */
    @DeleteMapping
    @PreAuthorize("hasAuthority('config_delete')")
    @ApiOperation(value = "删除平台参数配置", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "ids", value = "配置 ID 数组", dataType = "Integer", required = true, paramType = "query", example = "1")
    )
    public Result<Void> delete(@RequestParam("ids") @NotEmpty List<Long> ids) {
        boolean flag = configService.removeByIds(ids);
        // 如果为 true 删除成功
        if (flag) return Result.success("删除成功");
        // 如果为 false 删除失败
        return Result.fail("删除失败");
    }
}
